/*
 * @FilePath: /tads/server/app/business/apis/tactics_topo.go
 * @Author: wnina0x0
 * @Date: 2025-04-17 16:40:26
 * @Description:
 *
 * 版权信息 : 2025 by wnina0x0, All Rights Reserved.
 */
// /*
//  * @FilePath: /tads/server/app/business/apis/scene_topo.go
//  * @Author: wnina0x0
//  * @Date: 2025-04-17 16:40:26
//  * @Description:
//  *
//  * 版权信息 : 2025 by wnina0x0, All Rights Reserved.
//  */
package apis

import (
	"fmt"
	"net/http"
	"tads/server/app/business/models"
	"tads/server/app/business/service"
	"tads/server/app/business/service/dto"
	"tads/server/core/sdk/api"

	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

type TacticTopo struct {
	api.Api
}

func (e TacticTopo) Get(c *gin.Context) {
	req := dto.TacticsTopoGetRequests{}
	s := service.SceneTopo{}
	err := e.MakeContext(c).MakePostgres().Bind(&req, binding.Query).MakeService(&s.Service).Errors
	if err != nil {
		e.Logger.Errorf("请求参数错误: %s!\n", err.Error())
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprintf("请求参数错误: %s!\n", err.Error())})
		return
	}
	if req.ID == 0 {
		e.Logger.Errorf("请求参数不能为空: %s!\n", fmt.Errorf("请求参数不能为空"))
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprintf("请求参数不能为空: %s!\n", fmt.Errorf("请求参数不能为空"))})
		return
	}
	res := models.TacticsTopo{}
	if err = s.Get(&req, &res); err != nil {
		e.Logger.Errorf("拓扑获取错误: %s!\n", err.Error())
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprintf("拓扑获取错误: %s!\n", err.Error())})
		return
	}
	e.Logger.Info("拓扑获取成功")
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "拓扑获取成功",
		"data": res,
	})
	return
}

func (e TacticTopo) Inset(c *gin.Context) {
	req := dto.TacticsTopoInsetRequests{}
	s := service.SceneTopo{}
	err := e.MakeContext(c).MakePostgres().Bind(&req, binding.JSON).MakeService(&s.Service).Errors
	if err != nil {
		e.Logger.Errorf("请求参数错误: %s!\n", err.Error())
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprintf("请求参数错误: %s!\n", err.Error())})
		return
	}
	if req.TacticsTopoData == "" || req.TacticsTopoDesc == "" {
		e.Logger.Errorf("插入失败:%s\n", fmt.Errorf("缺少参数"))
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprintf("请求参数错误: %s!\n", fmt.Errorf("缺少参数"))})
		return
	}
	if err = s.Inset(&req); err != nil {
		e.Logger.Errorf("插入失败:%s\n", err.Error())
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprintf("请求参数错误: %s!\n", err.Error())})
		return
	}
	e.Logger.Info("拓扑添加成功")
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "拓扑添加成功"})
	return
}

func (e TacticTopo) Update(c *gin.Context) {
	req := dto.TacticsTopoUpdateRequests{}
	s := service.SceneTopo{}
	err := e.MakeContext(c).MakePostgres().Bind(&req, binding.JSON).MakeService(&s.Service).Errors
	if err != nil {
		e.Logger.Errorf("请求参数错误: %s!\n", err.Error())
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprintf("请求参数错误: %s!\n", err.Error())})
		return
	}
	if err = s.Update(&req); err != nil {
		e.Logger.Errorf("拓扑修改错误:%s\n", err.Error())
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprintf("拓扑修改错误: %s!\n", err.Error())})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "拓扑修改错误",
	})
}
